﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _18miljard.Menu;

namespace _18miljard.UI.Controls
{
	public partial class MenuControl : System.Web.UI.UserControl
	{
		string menuName = "visitor";
		MainMenu mainMenu;
		int menuLevel = 0;

		protected override void Render(HtmlTextWriter writer)
		{
			if (string.IsNullOrEmpty(this.menuName))
				return;

			this.mainMenu = MenuCache.Menus[menuName];

			if (this.mainMenu == null)
				return;

			//writer.Write("<script language=\"JavaScript\" type=\"text/javascript\" src=\"/Script/elementwork.js\"></script>");

			this.RenderMenu(this.mainMenu, writer);
		}

		private void RenderMenu(MainMenu menu, HtmlTextWriter writer)
		{
			this.menuLevel = 0;

			writer.WriteBeginTag("div");
			if (menu.Direction == MenuDirection.Horizontal)
				this.RenderCSS("menu", writer);
			else
				this.RenderCSS("menudown", writer);

			writer.Write(HtmlTextWriter.TagRightChar);

			this.RenderItems(menu, writer, false);

			writer.WriteEndTag("div");
		}

		private void RenderItems(_18miljard.Menu.MenuItem menu, HtmlTextWriter writer, bool sub)
		{
			this.menuLevel++;

			writer.WriteBeginTag("ul");

			writer.Write(HtmlTextWriter.TagRightChar);

			foreach (_18miljard.Menu.MenuItem item in menu)
			{
				this.RenderItem(item, writer);
			}

			writer.WriteEndTag("ul");

			this.menuLevel--;
		}

		private void RenderItem(_18miljard.Menu.MenuItem item, HtmlTextWriter writer)
		{
			writer.WriteBeginTag("li");

			if (item.IsSeparator)
			{
				if (this.menuLevel == 1) //must be a vertical menu
					this.RenderCSS("separator", writer);

				writer.Write(HtmlTextWriter.TagRightChar);

				if (this.menuLevel > 1)
					writer.Write("<img src=\"/Images/GreyPixel.gif\" alt=\"\" style=\"width: 122px; height: 1px; margin-left:3px; margin-top: 4px; margin-bottom: 4px;\"/>");
			}
			else
			{
				if (string.IsNullOrEmpty(item.ItemCss) == false)
				{
					this.RenderCSS(item.ItemCss, writer);
				}

				writer.Write(HtmlTextWriter.TagRightChar);

				string itemText = item.Text;

				if (string.IsNullOrEmpty(item.Href) && item.Count == 0)
				{
					writer.WriteBeginTag("span");
					writer.Write(HtmlTextWriter.TagRightChar);
					writer.Write(itemText);
					writer.WriteEndTag("span");
				}
				else
				{
					writer.WriteBeginTag("a");
					if (item.Count == 0)
					{
						if (string.IsNullOrEmpty(item.Href) == false)
							writer.WriteAttribute("href", item.Href);
						else
							writer.WriteAttribute("href", "#");

						if (string.IsNullOrEmpty(item.Target) == false)
							writer.WriteAttribute("target", item.Target);
					}
					else
						writer.WriteAttribute("href", "#");

					writer.Write(HtmlTextWriter.TagRightChar);

					writer.Write(itemText);
					if (item.Count > 0)
						writer.Write("&nbsp;»");

					writer.WriteEndTag("a");

					if (item.Count > 0)
					{
						this.RenderItems(item, writer, true);
					}
				}
			}

			writer.WriteEndTag("li");
		}

		/*************/
		private void RenderCSS(string className, HtmlTextWriter writer)
		{
			if (string.IsNullOrEmpty(className) == false)
				writer.WriteAttribute("class", className);
		}

		public string MenuName
		{
			get { return this.menuName; }
			set { this.menuName = value; }
		}
	}
}